<!doctype html>
 <html>
 	<head>
 		<title>dsaf</title>
 		<meta name="renderer" content="webkit">
 		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
 		<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no" />
 		<meta charset="utf-8">
 		<script src="/static/common/js/jquery.min.js"></script>
 	<body>
		<input type="text" id="key" value="0" />
 		<button id="btn">开始</button>
		<div>进度：<span class="por"></span>/<span class="total">{$count}</span></div>
 		<div id="sd"></div>

 		<script>
			var count='{$count}'
 			//写入书
 			$("#btn").click(function() {
				var key=$("#key").val();
				console.log(key)
 				writeBooks(key)
 			})

 			function writeBooks(j) {
 				new Promise(function(resolve, reject) {
 					$.ajax({
 						url: '{:url("/index/xiaoshuo.index/getBase")}',
 						type: 'get',
 						data: 'start=' + j,
 						success: function(res) {

 							resolve(res)
 						}
 					})
 				}).then(res => {
 					let data = res.data[0];
			
         sendurl(data.url,data.id,res.count)
 					console.log(data)

 				})
 			}

 			function sendurl(base_url,id,nums) {
 				console.log(nums)
 				new Promise(function(resolve, reject) {
 					$.ajax({
 						url: '{:url("/index/xiaoshuo.index/writeBooks")}',
 						type: 'post',
 						data: {
							 'url':base_url,
							 'id':id
						},
 						success: function(res) {

 							resolve(res)
 						}
 					}).then(res => {
 						console.log(res)
 						 $(".por").html(nums)
						 if(nums <=count){
 						//writeBooks(nums)
						}
 					})
 				})
 			}

 			//获取源书地址
 			var i = 1;
 			var nums = 1708;
			//$(".total").html(nums)
			var k=10;
 			var url = 'http://www.mhtwx.la/mulu/'+k+'-' + i + '.html';
      //send(url,k)
 			function send(url,k) {
 				new Promise(function(resolve, reject) {
 					$.ajax({
 						url: '{:url("/index/xiaoshuo.index/setlist")}',
 						type: 'get',
 						data: 'url=' + url+"&k="+k,
 						success: function(res) {
 							i++;
 							console.log(i)
 							resolve(res)
 						}
 					})
 				}).then(res => {
 					console.log(res)
 					if (i < nums) {
						$(".por").html(i)
 						send('http://www.mhtwx.la/mulu/'+k+'-' + i + '.html',k)
 					}else{
						console.log('已结束')
					}
 				})
 			}
 		</script>

 	</body>
